<?php
	$controller = $this->params['controller'];
	$action = $this->params['action'];
	
	$sort_by = (!empty($this->params['named']['sort_by'])) ? $this->params['named']['sort_by'] : '';
	
	$priority_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'priority_desc') ? 'priority_asc' : 'priority_desc');
	$name_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'name_desc') ? 'name_asc' : 'name_desc');
	$project_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'project_desc') ? 'project_asc' : 'project_desc');
	$status_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'status_desc') ? 'status_asc' : 'status_desc');
	$performer_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'performer_desc') ? 'performer_asc' : 'performer_desc');
	$modified_sort_url = array('controller' => $controller, 'action' => $action, 'sort_by' => ($sort_by == 'modified_desc') ? 'modified_asc' : 'modified_desc');
	
?>
<?php if ($controller == 'tasks' && $action == 'index') : ?>
<tr>
	<td></td><td></td>
	<td>
		<?php echo $form->input('Filter.status', array(
			'type' => 'select',
			'options' => array('open' => 'open', 'close' => 'close', 'all' => 'all'),
			'label' => false,
			'div' => false,
			'style' => 'width: 100%',
			'default' => $selected_status
		)); ?>
	</td>
	<td>
		<?php echo $form->input('Filter.user', array(
			'type' => 'select',
			'options' => $users_in_project,
			'label' => false,
			'div' => false,
			'style' => 'width: 100%',
			'default' => $selected_user
		)); ?>
	</td>
	<td></td>
</tr>
<?php endif; ?>
<tr class="tasks-header">
	<td class="priority <?php if (preg_match('/priority/', $sort_by)) {echo "sort_by";} ?>">
		<a href="<?php echo $html->url($priority_sort_url); ?>"><div style="width: 100%; height: 20px;"></div></a>
	</td>
	<td class="name <?php if (preg_match('/name/', $sort_by)) {echo "sort_by";} ?>">
		<a href="<?php echo $html->url($name_sort_url); ?>"><div>Task name</div></a>
	</td>
	<?php if (!empty($project_column)) : ?>
	<td class="project <?php if (preg_match('/project/', $sort_by)) {echo "sort_by";} ?>">
		<a href="<?php echo $html->url($project_sort_url); ?>"><div>Project</div></a>
	</td>
	<?php endif; ?>
	<td class="status <?php if (preg_match('/status/', $sort_by)) {echo "sort_by";} ?>">
		<a href="<?php echo $html->url($status_sort_url); ?>"><div>Status</div></a>
	</td>
	<?php if (!empty($performer_column)) : ?>
	<td class="performer <?php if (preg_match('/performer/', $sort_by)) {echo "sort_by";} ?>">
		
		<a href="<?php echo $html->url($performer_sort_url); ?>"><div>Performer</div></a>
	</td>
	<?php endif; ?>
	<td class="modified <?php if (preg_match('/modified/', $sort_by)) {echo "sort_by";} ?>">
		<a href="<?php echo $html->url($modified_sort_url); ?>"><div>Update</div></a>
	</td>
</tr>
<?php foreach($tasks as $i => $task) : ?>
<tr class="task">
	<td class="priority-<?php echo $task['Task']['priority']; ?>"></td>
	<td class="name <?php echo ($i % 2 ==0 ) ? 'not-even' : 'even'; ?>">
		<?php echo $this->element('tracking_panel', array('task' => $task)); ?>
		<a href="/tasks/view/<?php echo $task['Task']['id']; ?>">
			<?php if ($task['Task']['status'] == 'closed') { echo '<s>'; }; ?>
				<div><?php echo $task['Task']['name']; ?></div>
			<?php if ($task['Task']['status'] == 'closed') { echo '</s>'; }; ?>
		</a>
	</td>
	<?php if (!empty($project_column)) : ?>
	<td class="project <?php echo ($i % 2 ==0 ) ? 'not-even' : 'even'; ?>">
		<a href="/tasks/index/<?php echo $task['Project']['id']; ?>"><div><?php echo $task['Project']['name']; ?></div></a>
	</td>
	<?php endif; ?>
	<td class="status <?php echo ($i % 2 ==0 ) ? 'not-even' : 'even'; ?>">
		<?php echo $task['Task']['status']; ?>
	</td>
	<?php if (!empty($performer_column)) : ?>
	<td class="performer <?php echo ($i % 2 ==0 ) ? 'not-even' : 'even'; ?>">
		<?php echo $task['Performer']['username']; ?>
	</td>
	<?php endif; ?>
	<td class="modified <?php echo ($i % 2 ==0 ) ? 'not-even' : 'even'; ?>">
		<?php echo $task['Task']['modified']; ?>
	</td>
</tr>
<?php endforeach; ?>
<script>
	$("#FilterStatus, #FilterUser").change(function(){
		$("form#FilterIndexForm").submit();
	});
</script>